package org.itboys.mobile.service.mongo.mobile;


import org.apache.velocity.util.ArrayListWrapper;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.kitchen.entity.mongo.village.KitchenVillage;
import org.itboys.kitchen.entity.mysql.income.KitchenIncome;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.promotion.SundayCouponMapper;
import org.itboys.mobile.dto.count.KitchenCountDto;
import org.itboys.mobile.dto.score.SundayMemberFinanceDto;
import org.itboys.mobile.entity.mongo.KitchenCart;
import org.itboys.mobile.entity.mongo.SundayCid;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.promotion.SundayCoupon;
import org.itboys.mobile.entity.mysql.score.SundayScore;
import org.itboys.mobile.service.mysql.mobile.income.KitchenMobileIncomeService;
import org.itboys.mobile.service.mysql.mobile.promotion.SundayMobilePromotionService;
import org.itboys.mobile.service.mysql.mobile.score.SundayMobileScoreService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_结算（确认订单）_service
 */
@Service
public class KitchenMobileCountService{
    @Autowired
    private KitchenMobileCartService cartService;
    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private KitchenMobileIncomeService incomeService;

    @Autowired
    private KitchenMobileExpressModeService expressModeService;
    @Autowired
    private KitchenMobileVillageService villageService;
    @Autowired
    private SundayMobileMemberService memberService;
    @Autowired
    private SundayMobileScoreService scoreService;
    /**
     * 购物车结算。双版本通用
     * @param memberId
     * @param cartIds
     * @param isUsedBalance
     * @param couponId
     * @return
     */
    public KitchenCountDto countCart(Long memberId,List<Long> cartIds,Integer isUsedBalance,Long couponId,Integer cartType,Integer sendType,Long villageId){
        isUsedBalance=isUsedBalance==null?0:isUsedBalance;
        couponId= LongUtil.isNotZreo(couponId)?couponId:0l;
        KitchenCountDto countDto = cartService.getCart(memberId,cartIds,cartType,villageId);
        //下单用户的账户信息
        SundayMemberFinanceDto memberFinanceDto=memberService.getMemberFinance(memberId,null);
       //用户积分
        countDto.setScore(memberFinanceDto.getScore());
        //用户余额
        countDto.setBalance(memberFinanceDto.getBalance());
        //用户折扣
        countDto.setDiscount(memberFinanceDto.getDiscount());

        //结算对应的门店信息
        if(LongUtil.isNotZreo(villageId)){
            KitchenVillage village  =villageService.getById(villageId);
            SundayMember shop = memberService.getById(village.getShopId());
            countDto.setShopId(shop.getId());
            countDto.setShopName(shop.getNickName());
            countDto.setSendAddress(shop.getAddress());
            countDto.setVillageId(villageId);
            countDto.setVillageName(village.getName());
        }
        BigDecimal totalAmount = countDto.getTotalAmount();
        BigDecimal payAmount = countDto.getPayAmount();

        //下单类型的配单。1商城，2积分，3采购。
        //商城结算
        if(countDto.getType()==SundayCommonConstants.type_mall){
            //根据客户绑定的会员卡打折
            payAmount=payAmount.multiply(countDto.getDiscount());

            countDto.setSendType(sendType);
            //1,匹配运费和运费信息
            if(sendType==SundayCommonConstants.send_type_send){
                Map<String,Object> result = expressModeService.matching(payAmount.doubleValue());
                BigDecimal sendAmount =(BigDecimal)result.get("sendAmount");
                String sendMsg= (String)result.get("sendMsg");
                totalAmount=totalAmount.add(sendAmount);
                payAmount=payAmount.add(sendAmount);
                countDto.setSendAmount(sendAmount);
                countDto.setSendMsg(sendMsg);
            }
            //使用优惠券
            if(LongUtil.isNotZreo(couponId)){
                SundayCoupon coupon= couponMapper.find(couponId);
                payAmount=payAmount.subtract(coupon.getDecuAmount());
                countDto.setCouponId(couponId);
                countDto.setCouponMsg("优惠券已减"+coupon.getDecuAmount()+"元");
                countDto.setCouponAmount(coupon.getDecuAmount());
            }
            //使用余额抵扣
            if(isUsedBalance==1){
                countDto.setIsUserBalance(isUsedBalance);
                BigDecimal balance=countDto.getBalance();
                //1，结算金额大于余额、
                if(payAmount.compareTo(balance)==1){
                    countDto.setUsedBalance(balance);
                    countDto.setBalanceMsg("余额已抵扣："+balance+"元");
                    payAmount=payAmount.subtract(balance);
                    //2，结算金额小于余额
                }else if(payAmount.compareTo(balance)==0||
                        payAmount.compareTo(balance)==-1){
                    countDto.setUsedBalance(payAmount);
                    countDto.setBalanceMsg("余额已抵扣："+payAmount+"元");
                    payAmount=new BigDecimal("0.00");
                }
            }
            countDto.setMsg("已优惠"+countDto.getTotalAmount().subtract(countDto.getPayAmount())+"元");

        //积分结算。积分商品不打折
        }else if(countDto.getType()==SundayCommonConstants.type_score){

        //采购结算
        }else if(countDto.getType()==SundayCommonConstants.type_caigou){
            payAmount=payAmount.multiply(countDto.getDiscount());
        }
        countDto.setTotalAmount(totalAmount);
        countDto.setPayAmount(payAmount);
        return countDto;
    }


    /**
     * 立即购买结算
     * @param memberId
     * @param productId
     * @param number
     * @param isUsedBalance
     * @param couponId
     * @param villageId
     * @return
     */
    public KitchenCountDto countBuyNow(Long memberId,Long productId,Integer number,Integer isUsedBalance,Long couponId,Integer sendType,Long villageId){
        List<Long> productIds =new ArrayList<Long>();
        productIds.add(productId);
        List<Integer> numbers =new ArrayList<Integer>();
        numbers.add(number);
        List<Long> cartIds= cartService.addCart(memberId,productIds ,numbers, SundayCommonConstants.cart_type_buy_now,villageId);
        return countCart(memberId,cartIds,isUsedBalance,couponId,SundayCommonConstants.cart_type_buy_now,sendType,villageId);
    }


}
